package com.android.monee.view.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.res.Resources;

import com.android.monee.binding.Field;
import com.android.monee.binding.observable.Observable;
import com.android.monee.binding.observable.ObservableImpl;
import com.android.monee.binding.observer.Observer;
import com.android.monee.data.model.Budget;
import com.android.monee.domain.BudgetService;
import com.android.monee.R;


public class BudgetOverview extends BudgetService implements Observable<BudgetOverview> {

	private Resources res;
	private String currency;
	private Observable<BudgetOverview> observableDelegate;

	public BudgetOverview (Budget budget, Resources res){
		super(budget);
		constructor(res);
	}
	
	private void constructor(Resources res){
		this.res = res; 
		this.currency = res.getText(R.string.currency).toString();
		this.observableDelegate = new ObservableImpl<BudgetOverview>(this);
	}
	
	public String getAmmountTotProvLib() {
		return res.getText(R.string.mtn_prov_lbl) + " "
				+ getProvAmountFloat().intValue() + "/" + getTotalAmountFloat().intValue()
				+ currency;
	}

	public String getAmountMensLib() {
		return res.getText(R.string.mtn_mens_lbl) + " "
				+ getMensAmountFloat().intValue() + res.getText(R.string.currency);
	}

	public float getProgressBarWeight (){
		return getTotalAmount() - getProvAmount();
	}
	
	public float getProgressSpacerWeight (){
		return getProvAmount();
	}

	public Float getProvAmountFloat() {
		return super.getProvAmount();
	}

	public Float getMensAmountFloat() {
		return super.getMensAmount();
	}
	
	public Float getTotalAmountFloat(){
		return super.getTotalAmount();
	}
		
	
	@Override
	public void setState(BudgetState state) {
		super.setState(state);
		this.observableDelegate.notify(State.class);
	}

	public <T, V> void addObserver(Observer<BudgetOverview, T, V> o) {
		observableDelegate.addObserver(o);
	}

	public <T, V> void deleteObserver(Observer<BudgetOverview, T, V> o) {
		observableDelegate.deleteObserver(o);
	}

	public <T, V> void notify(Class<? extends Field> fieldClass) {
		observableDelegate.notify(fieldClass);
	}

	public BudgetOverview getObservedData() {
		return observableDelegate.getObservedData();
	}

	/*
	 * Question : est-ce qu'il serait préférable de transformer ces méthodes statiques en méthode d'objets ?
	 * Cela permettrait de ne pas avoir à passer 'res' en pramètre mais cela parait moins 'logique' 
	 */
	public static String getTotalMensAmountLib(Collection<? extends BudgetService> budgets, Resources res) {
		return res.getText(R.string.tot_mens_lbl).toString()
				+ BudgetService.calcTotalMensAmount(budgets).intValue()
				+ res.getText(R.string.currency).toString();
	}

	public static String getTotalProvAmountLib(Collection<? extends BudgetService> budgets, Resources res) {
		return res.getText(R.string.tot_prov_lbl).toString()
				+ BudgetService.calcTotalProvAmount(budgets).intValue()
				+ res.getText(R.string.currency).toString();
	}
	
	public static List<BudgetOverview> newCollection(Collection<? extends Budget> budgets, Resources res){
		List<BudgetOverview> newCol = new ArrayList<BudgetOverview>();
		for(Budget budget : budgets){
			newCol.add(new BudgetOverview(budget, res));
		}
		return newCol;
	}
	

	public class State implements Field<BudgetOverview, BudgetState>{
		
		@Override
		public BudgetState get(BudgetOverview obj) {
			return obj.getState();
		}
		
		@Override
		public void set(BudgetOverview obj, BudgetState val) {
			obj.setState(val);
		}
		
	}
}
